package com.yuda.campsite.controller;

import com.alibaba.fastjson.JSONObject;
import com.yuda.campsite.domain.PersonalFinance;
import com.yuda.campsite.service.PersonalFinanceService;
import com.yuda.campsite.service.impl.PersonalFinanceServiceImpl;
import com.yuda.common.core.utils.poi.ExcelUtil;
import com.yuda.common.core.web.domain.AjaxResult;
import com.yuda.common.core.web.domain.BaseEntity;
import com.yuda.common.datascope.annotation.DataScope;
import com.yuda.common.security.service.TokenService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: xiaohudie
 * @Date: 2021/11/30 10:32
 * @Version: 1.8
 */
@RestController
@RequestMapping("/finance")
@Slf4j
public class PersonalFinanceController {

    @Resource
    private PersonalFinanceService personalFinanceService;


    /**
     * 时间戳转化工具类
     */
    public static String timeStamp2Date(String seconds, String format) {
        if (seconds == null || seconds.isEmpty() || seconds.equals("null")) {
            return "";
        }
        if (format == null || format.isEmpty()) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(seconds + "000")));
    }

    //TODO 控制数据权限

    /**
     * 查询支付过的个人
     */
    @DataScope(deptAlias = "f")
    @GetMapping("/selectld")
    public String selectld(BaseEntity baseEntity,
                           @RequestParam(value = "provinceName", required = false) String provinceName,
                           @RequestParam(value = "campsiteName", required = false) String campsiteName,
                           @RequestParam(value = "cityName", required = false) String cityName,
                           @RequestParam(value = "phone", required = false) String phone,
                           @RequestParam(value = "idCard", required = false) String idCard,
                           @RequestParam(value = "qisi", defaultValue = "1") Integer qisi,
                           @RequestParam(value = "offset", defaultValue = "10") Integer offset) {
        JSONObject jsonObject = new JSONObject();
        int size = personalFinanceService.getCount(baseEntity.getParams(), provinceName, campsiteName, cityName, phone, idCard,"6");
        List<PersonalFinance> personalFinances = personalFinanceService.selectPersonalFinance(baseEntity.getParams(), provinceName, campsiteName, cityName, phone, idCard, (qisi - 1) * offset, offset,"6");
        List<PersonalFinance> collect = personalFinances.stream().peek(x -> {
            x.setStatus("已支付");
            x.setPayTime(timeStamp2Date(x.getPayTime(), null));
        }).collect(Collectors.toList());
        jsonObject.put("code", 200);
        jsonObject.put("message", "查询成功");
        jsonObject.put("total", size);
        jsonObject.put("data", collect);
        return jsonObject.toJSONString();
    }

    //TODO 控制数据权限

    /**
     * 查询未支付的个人
     */
    @DataScope(deptAlias = "f")
    @GetMapping("/selectWZFld")
    public String selectWZFld(BaseEntity baseEntity,
                              @RequestParam(value = "provinceName", required = false) String provinceName,
                              @RequestParam(value = "campsiteName", required = false) String campsiteName,
                              @RequestParam(value = "cityName", required = false) String cityName,
                              @RequestParam(value = "phone", required = false) String phone,
                              @RequestParam(value = "idCard", required = false) String idCard,
                              @RequestParam(value = "qisi", defaultValue = "1") Integer qisi,
                              @RequestParam(value = "offset", defaultValue = "10") Integer offset) {

        JSONObject jsonObject = new JSONObject();
        int size = personalFinanceService.getCount(baseEntity.getParams(), provinceName, campsiteName, cityName, phone, idCard,"2");
        List<PersonalFinance> personalFinances = personalFinanceService.selectPersonalFinance(baseEntity.getParams(), provinceName, campsiteName, cityName, phone, idCard, (qisi - 1) * offset, offset,"2");

        for (PersonalFinance personalFinance : personalFinances) {
            personalFinance.setStatus("未支付");
        }
        jsonObject.put("code", 200);
        jsonObject.put("message", "查询成功");
        jsonObject.put("total", size);
        jsonObject.put("data", personalFinances);
        return jsonObject.toJSONString();
    }

    @DataScope(deptAlias = "f")
    @PostMapping("/ExportYZF")
    public void ExportYZF(BaseEntity baseEntity, HttpServletResponse httpServletResponse) {
        List<PersonalFinance> personalFinances = personalFinanceService.selectPersonalFinance(baseEntity.getParams(), null, null, null, null, null, 0, 10000,"6");
        List<PersonalFinance> collect = personalFinances.stream().filter(x ->x.getStatus()!=null&& x.getStatus().equals("6")).collect(Collectors.toList());
        for (PersonalFinance personalFinance : collect) {
            personalFinance.setStatus("已支付");
            personalFinance.setPayTime(timeStamp2Date(personalFinance.getPayTime(),null));
            if (personalFinance.getPayWay().equals("1")){
                personalFinance.setPayWay("银联");
            }if (personalFinance.getPayWay().equals("2")){
                personalFinance.setPayWay("支付宝");
            }if (personalFinance.getPayWay().equals("3")){
                personalFinance.setPayWay("微信");
            }
        }
        ExcelUtil<PersonalFinance> personalFinanceExcelUtil = new ExcelUtil<>(PersonalFinance.class);
        try {
            personalFinanceExcelUtil.exportExcel(httpServletResponse, collect, "finance");
        } catch (IOException e) {
            log.info("下载财务管理中的已支付excel表失败");
            e.printStackTrace();
        }
    }

    @DataScope(deptAlias = "f")
    @PostMapping("/ExportWZF")
    public void ExportWZF(BaseEntity baseEntity, HttpServletResponse httpServletResponse) {
        List<PersonalFinance> personalFinances = personalFinanceService.selectPersonalFinance(baseEntity.getParams(), null, null, null, null, null, 0, 10000,"2");
        List<PersonalFinance> collect = personalFinances.stream().filter(x ->x.getStatus()!=null && !x.getStatus().equals("6")).collect(Collectors.toList());

        for (PersonalFinance personalFinance : collect) {
            personalFinance.setPayWay(" ");
           personalFinance.setStatus("未支付");
            personalFinance.setPayTime(timeStamp2Date(personalFinance.getPayTime(),null));

        }
        ExcelUtil<PersonalFinance> personalFinanceExcelUtil = new ExcelUtil<>(PersonalFinance.class);
        try {
            personalFinanceExcelUtil.exportExcel(httpServletResponse, collect, "finance");
        } catch (IOException e) {
            log.info("下载财务管理中的未支付excel表失败");
            e.printStackTrace();
        }
    }
    /**退款按钮*/
    @PostMapping("/SbYaZh")
    public AjaxResult SbYangZhao(@RequestParam(value = "orderNo") String orderNo){
        personalFinanceService.FwYangZhao(orderNo);
        return AjaxResult.success("退款成功");
    }
    /**
     * 查询已退款的个人
     */
   /* @DataScope(deptAlias = "f")*/
    @GetMapping("/selectcwy")
    public String selectcwy(BaseEntity baseEntity,
                           @RequestParam(value = "provinceName", required = false) String provinceName,
                           @RequestParam(value = "campsiteName", required = false) String campsiteName,
                           @RequestParam(value = "cityName", required = false) String cityName,
                           @RequestParam(value = "phone", required = false) String phone,
                           @RequestParam(value = "idCard", required = false) String idCard,
                           @RequestParam(value = "qisi", defaultValue = "1") Integer qisi,
                           @RequestParam(value = "offset", defaultValue = "10") Integer offset) {
        JSONObject jsonObject = new JSONObject();
        int size = personalFinanceService.getCount(baseEntity.getParams(), provinceName, campsiteName, cityName, phone, idCard,"7");
        List<PersonalFinance> personalFinances = personalFinanceService.selectPersonalFinance(baseEntity.getParams(), provinceName, campsiteName, cityName, phone, idCard, (qisi - 1) * offset, offset,"7");
        List<PersonalFinance> collect = personalFinances.stream().peek(x -> {
            x.setStatus("已退款");
            x.setPayTime(timeStamp2Date(x.getPayTime(), null));
        }).collect(Collectors.toList());
        jsonObject.put("code", 200);
        jsonObject.put("message", "查询成功");
        jsonObject.put("total", size);
        jsonObject.put("data", collect);
        return jsonObject.toJSONString();
    }
    @DataScope(deptAlias = "f")
    @PostMapping("/ExportYTK")
    public void ExportYTK(BaseEntity baseEntity, HttpServletResponse httpServletResponse) {
        List<PersonalFinance> personalFinances = personalFinanceService.selectPersonalFinance(baseEntity.getParams(), null, null, null, null, null, 0, 10000,"7");
        List<PersonalFinance> collect = personalFinances.stream().filter(x ->x.getStatus().equals("7")).collect(Collectors.toList());

        for (PersonalFinance personalFinance : collect) {
            if (personalFinance.getPayWay().equals("1")){
                personalFinance.setPayWay("银联");
            }if (personalFinance.getPayWay().equals("2")){
                personalFinance.setPayWay("支付宝");
            }if (personalFinance.getPayWay().equals("3")){
                personalFinance.setPayWay("微信");
            }
            personalFinance.setStatus("已退款");
            personalFinance.setPayTime(timeStamp2Date(personalFinance.getPayTime(),null));

        }
        System.out.println("************" +collect);
        ExcelUtil<PersonalFinance> personalFinanceExcelUtil = new ExcelUtil<>(PersonalFinance.class);
        try {
            personalFinanceExcelUtil.exportExcel(httpServletResponse, collect, "finance");
        } catch (IOException e) {
            log.info("下载财务管理中的未支付excel表失败");
            e.printStackTrace();
        }
    }
}
